/* !点击logo回到首页 */
let logoimg = document.querySelector('.logoimg');
logoimg.onclick = () => {
  location.href = '../html/huaindex.html';
}
let login = getCookie('login');
if (!login) {
        localStorage.setItem('url', location.href);
        location.href = '../html/login.html';
}
let arr3 = [];
ajax({
    url: '/flo/getCardata.php',
    data: {
        phonenum: login,
    },
    success: function (res) {
        res = JSON.parse(res);
        let r = localStorage.getItem('data');
        if (!r || !res.detail) {
            return;
        }
        r = JSON.parse(r);
        r.forEach((item1) => {
            res.detail.forEach((item2) => {
                if (item1.ItemCode == item2.goods_id) {
                    let obj = Object.assign(item1, item2);
                    arr3.push(obj);
                }
            })
        })
     let gwcnum = document.querySelector('.gwcnum');
        gwcnum.innerHTML=`(${res.detail.length})`
        li(arr3);
    }
})

/* !渲染商品数据 */
function li(data) {
    let shoplists = document.querySelector('.shoplists');
    shoplists.innerHTML = data.map((item,index) => {
        return ` <li index="${item.ItemCode}">
                        <input type="checkbox" name="" class="check" ${item.is_select=='1'?'checked':''}>
                        <img src="${item.img[0]}" alt="">
                        <p>[${item.xianhua}]${item.Cpmc}-${item.Instro}</p>
                        <span class="oldpri">￥${item.LinePrice}</span>
                        <span class="nowpri">￥${item.Price}</span>
                         <div class="num">
                           <div class="btn-group" index=${index}>
                                    <button class="btn1" ${item.goods_num == 1 ? "disabled" : ""}>
                                        -
                                    </button>
                                    <button class="btn">
                                        ${item.goods_num}
                                    </button>
                                    <button class="btn2">
                                        +
                                    </button>
                                </div>
                         </div>
                        <div class="caozuo">
                            <button class="del">删除</button>
                            <button class="move">移到我的收藏</button>
                        </div>
                    </li>`
    }).join('');
}

/* !操作数据列表 */
let shoplists = document.querySelector('.shoplists');
shoplists.onclick = (e) => {
    var e = e || window.event;
    if (e.target.classList.contains('del')) {
        let idx = e.target.parentNode.parentNode.getAttribute("index");
        if (!confirm('确定要删除该商品吗？')) return;     
        let p = pAjax({
            url: '/flo/removeCarData.php',
            data: {
                phonenum: login,
                goods_id: idx
            }
        });
        p.then(res => {
            res = JSON.parse(res);
            if (res.code) {
              arr3 = arr3.filter((item) => {
                     return  item.ItemCode != idx
                 })
                li(arr3);
                 sum();
            }
             
        })
    }
    if (e.target.classList.contains('btn1')) {
        let idx = e.target.parentNode.getAttribute("index");
       arr3[idx].goods_num -= 1;
        updata(idx, arr3[idx].goods_num);
        sum();
        li(arr3);
    }
    if (e.target.classList.contains('btn2')) {
        
         let idx = e.target.parentNode.getAttribute("index");
       arr3[idx].goods_num = arr3[idx].goods_num*1+1;
        updata(idx,  arr3[idx].goods_num);
        sum();
        li(arr3);
    }
    if (e.target.classList.contains('check')) {
        let idx = e.target.parentNode.getAttribute("index");
        arr3.forEach((item) => {
            if (item.goods_id == idx) {
                item.is_select = e.target.checked ? "1" : "0";
            }
        })
        sum();
        li(arr3);
    }
}

/* !更新数据库 */
function updata(idx, num) {
    let p=pAjax({
        url: '/flo/updCarData.php',
        data: {
            phonenum: login,
            goods_id: idx,
            goods_num: num
        },
    });
    p.then(res => {
            res = JSON.parse(res);
            if (!res.code) return;
     arr3.forEach((item) => {
        if (item.goods_id == idx) {
            item.goods_num = num;
        }
     });
    li(arr3);
    })   
}
/* !计算总数 */
function sum() {
    let totalprice = document.querySelector('.totalprice');
    let total=arr3.reduce((pre, item) => {
     if (item.is_select == "1") {
        return pre + item.goods_num * item.Price;
    }
    return pre;
      }, 0)
        totalprice.innerHTML = '￥'+total;
}

/* !点击登录 */
let denglu = document.querySelector('.denglu');
denglu.onclick = (() => {
/* !判断是否有login */
let login = getCookie('login');
if (login) {
    alert('您已经登录啦');
    return
}
})




